********************************************************************************
***                                Preamble                                  ***
********************************************************************************
* Project: Counter-Punishment
* Procedure: Clean Data Set
* version 15
clear all
macro drop _all
********************************************************************************

************************
* IMPORTING FROM CSV *
************************

forvalues i = 1(1)5{
	clear
	import delimited using session_`i'.csv
	gen ses = `i'
	if `i'> 1 append using counterpunish_choices.dta
	sleep 2000
	save counterpunish_choices.dta, replace
}

******************
* CREATE DATASET *
******************
	
use counterpunish_choices.dta, clear

label data "Data 3-Party Counter-Punishment"

keep ses date subject group type take takeb pun0 pun5 pun10 puna0 puna5 puna10 ///
	red0 red10 red20 red30 red40 red50 red60 red70 red80 red90 red100 ///
	redb0 redb10 redb20 redb30 redb40 redb50 redb60 redb70 redb80 redb90 redb100 ///
	profita profitb

*********************
* GENERAL VARIABLES *
*********************
destring date, replace
	rename date session_code
	la var session_code "Session code"	

ren ses session	
	la var session "Session"	
	la def sessionlbl 1 "S1:3P-2P" 2 "S2:2P-3P" 3 "S3:2P-3P" ///
		 4 "S4:3P-2P" 5 "S5:2P-3P"
		 la val session sessionlbl

gen treat=.
	replace treat=1 if session==1
	replace treat=2 if session==2
	replace treat=2 if session==3
	replace treat=1 if session==4
	replace treat=2 if session==5	
	la var treat "Treatment"
	la def treatlbl 1 "Second-party" 2 "Third-party"
	la val treat treatlbl
	
ren group grp	
gen group=.
	replace group= 1000*session + 100*(treat) + grp
	la var group "Group"
		drop grp		

ren subject player
	la var  player "Player number in session"
	
gen subject= 10000*session + player
	la var subject "subject unique id in exp."
	
la var type "Role"
la def typelbl 1 "Player A" 2 "Player B"
la val type typelbl

**********************
* DECISION VARIABLES *
**********************
// STAGE 1: A's choice to TAKE from B's endowment
ren take tmp_take
gen take=.
replace take = tmp_take if type==1
replace take = takeb if type==2
la var take "A's take decision"
	drop  tmp_take takeb
	
tabulate take, gen(take)
ren take1 take0
	la var take0 "Dummy take 0"
ren take2 take5
	la var take5 "Dummy take 5"
ren take3 take10
	la var take10 "Dummy take 10"	
	
gen take_prob=1
	replace take_prob=0 if take==0 
		la var take_prob "Likelihood of taking"
	
gen exec_take=.
	replace exec_take=take
	la var take "A's executed take"
	

forvalues i=0(5)10{
	gen exec_take`i'=0
		replace exec_take`i'=1 if exec_take==`i'
		la var exec_take`i' "Dummy executed take `i'"
}

	
// STAGE 2: B's choice to PUNISH A
forvalues i = 0(5)10{
	gen punish`i'=.
		replace punish`i'=pun`i' if type==2
		replace punish`i'=puna`i' if type==1	
			la var punish`i' "Punishment by B if A took `i' EMU"
				drop pun`i' puna`i'
}

gen exec_punish=.
	replace exec_punish=punish0 if take==0
	replace exec_punish=punish5 if take==5
	replace exec_punish=punish10 if take==10
		la var exec_punish "B's executed punishment"

forvalues i = 0(5)10{
	gen exec_punish`i'=.
		replace exec_punish`i'=exec_punish if exec_take`i'==1
		la var exec_punish`i' "Executed punishment if A took `i'"
}
		
forvalues i = 0(5)10{
	gen punish_prob`i'=.
		replace punish_prob`i'=0 if punish`i'==0
		replace punish_prob`i'=1 if punish`i'>0
			la var punish_prob`i' "Likelihood of Punishment by B if A took `i' EMU"
}
	
gen exec_punish_prob=0
	replace exec_punish_prob=1 if exec_punish>0
		la var exec_punish_prob "B's likelihood to execute punishment"
	
forvalues i = 0(5)10{
	gen exec_punish_prob`i'=.
		replace exec_punish_prob`i'=0 if exec_punish`i'==0 & exec_take`i'==1
		replace exec_punish_prob`i'=1 if exec_punish`i'>0 & exec_take`i'==1
	
}
	
// STAGE 3: A's choice to COUNTER-PUNISH B
forvalues i = 0(10)100{
	gen counter`i'=.
		replace counter`i'=red`i' if type==1
		replace counter`i'=redb`i' if type==2
			la var counter`i' "Counter-punishment by A if B punished `i'%"
				drop red`i' redb`i'
}

gen exec_counter=.
	forvalues i=0(10)100{
		replace exec_counter=counter`i' if exec_punish==`i'
	}
	la var exec_counter "A's executed counter punishment"
	
forvalues i = 0(10)100{
	gen exec_counter`i'=.
		replace exec_counter`i'=exec_counter if exec_punish==`i'
		la var exec_counter`i' "Executed Counter-punishment by A if B punished `i'%"
}	
	
	
forvalues i = 0(10)100{
	gen counter_prob`i'=.
		replace counter_prob`i'=0 if counter`i'==0
		replace counter_prob`i'=1 if counter`i'>0
			la var counter_prob`i' "Likelihood of counter-punishment by A if B punished `i'%"
}

gen exec_counter_prob=0
	replace exec_counter_prob=1 if exec_counter>0
		la var exec_counter_prob "A's likelihood to execute counter-punishment"

forvalues i = 0(10)100{
	gen exec_counter_prob`i'=.
		replace exec_counter_prob`i'=0 if exec_counter`i'==0 & exec_punish==`i'
		replace exec_counter_prob`i'=1 if exec_counter`i'>0 & exec_punish==`i'
			la var exec_counter_prob`i' "Likelihood of executed counter-punishment by A if B punished `i'%"
}		
		
// PAYOFFS
gen pts=.
	replace pts=profita if type==1
	replace pts=profitb if type==2
		la var pts "Points"

gen tmp_pts=.
	replace tmp_pts= 30 + take if type==1
	replace tmp_pts= 30 - take if type==2
		
gen points=.
	replace points = ((100-exec_punish)/100)*tmp_pts + (5*(1-exec_punish_prob)) if type==1
	replace points = ((100-exec_counter)/100)*tmp_pts + (5*(1-exec_counter_prob)) if type==2
		la var points "Earnings"
	
***************
* SAVING DATA *
***************
// keep if part==1
// drop big* sequence part

sort treat session group type 

order session_code session treat group subject player type ///
	take* punish* counter* exec* pts points
	
save counterpunish_choices.dta, replace


************************
* IMPORTING FROM CSV *
************************

forvalues i = 1(1)5{
	clear
	import delimited using session_`i'_survey.csv
	gen ses = `i'
	if `i'> 1 append using counterpunish_survey.dta, force
	sleep 2000
	save counterpunish_survey.dta, replace
}

*************************
* CREATE DATASET SURVEY *
*************************	
use counterpunish_survey.dta, clear

keep ses date subject group f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 f21 f22 f23 f24 f25 f26 f27 f28 f29 f30 f31 f32 f33 f34 f35 f36 f37 f38 f39 f40 f41 f42 f43 f44 f45 f46 f47 f48 f49 f50 q1 q2 q3 q4 q5 q6 q7

*********************
* GENERAL VARIABLES *
*********************
destring date, replace
	rename date session_code
	la var session_code "Session code"	

ren ses session	
	la var session "Session"	
	la def sessionlbl 1 "S1:3P-2P" 2 "S2:2P-3P" 3 "S3:2P-3P" ///
		 4 "S4:3P-2P" 5 "S5:2P-3P"
		 la val session sessionlbl

gen treat=.
	replace treat=1 if session==1
	replace treat=2 if session==2
	replace treat=2 if session==3
	replace treat=1 if session==4
	replace treat=2 if session==5	
	la var treat "Treatment"
	la def treatlbl 1 "Second-party" 2 "Third-party"
	la val treat treatlbl
	
***
sort session subject treat
forvalues i = 1(1)50{
	by session subject: replace f`i' = f`i'[_n+1] if f`i' >= . & treat==2
	by session subject: replace f`i' = f`i'[_n+2] if f`i' >= . & treat==1	
// 	egen avg_f`i' = mean(f`i'), by(session Subject)	
}

forvalues i = 1(1)7{
	by session subject: replace q`i' = q`i'[_n+1] if q`i' >= . & treat==2
	by session subject: replace q`i' = q`i'[_n+2] if q`i' >= . & treat==1	
// 	egen avg_q`i' = mean(q`i'), by(session Subject)	
}
***

ren group grp	
gen group=.
	replace group= 1000*session + 100*(treat) + grp
	la var group "Group"
		drop grp		

ren subject player
	la var  player "Player number in session"
	
gen subject= 10000*session + player
	la var subject "subject unique id in exp."
	
		
************
* BIG FIVE *
************

forvalues i = 1(1)50{
	rename f`i' big`i'
	replace big`i' = big`i'+1 //values from 0 to 4 in zTree
}

forvalues i = 2(2)38{
	gen tmp`i' = big`i'
	replace big`i' = 1 if tmp`i' ==5
	replace big`i' = 2 if tmp`i' ==4
	replace big`i' = 3 if tmp`i' ==3
	replace big`i' = 4 if tmp`i' ==2
	replace big`i' = 5 if tmp`i' ==1
}

forvalues i = 29(10)40{
	gen tmp`i' = big`i'
	replace big`i' = 1 if tmp`i' ==5
	replace big`i' = 2 if tmp`i' ==4
	replace big`i' = 3 if tmp`i' ==3
	replace big`i' = 4 if tmp`i' ==2
	replace big`i' = 5 if tmp`i' ==1
}

forvalues i = 44(2)46{
	gen tmp`i' = big`i'
	replace big`i' = 1 if tmp`i' ==5
	replace big`i' = 2 if tmp`i' ==4
	replace big`i' = 3 if tmp`i' ==3
	replace big`i' = 4 if tmp`i' ==2
	replace big`i' = 5 if tmp`i' ==1
}

drop tmp*

gen open = big5 + big10 + big15 + big20 + big25 + big30 + big35 + big40 + big45 + big50 	
	la var open "Openness"	
	
gen conscient = big3 + big8 + big13 + big18 + big23 + big28 + big33 + big38 + big43 + big48 	
	la var conscient "Conscientiousness"	
	
gen extro = big1 + big6 + big11 + big16 + big21 + big26 + big31 + big36 + big41 + big46 
	la var extro "Extroversion"
	
gen agree = big2 + big7 + big12 + big17 + big22 + big27 + big32 + big37 + big42 + big47 	
	la var agree "Agreeableness"
	
gen neuro = big4 + big9 + big14 + big19 + big24 + big29 + big34 + big39 + big44 + big49 	
	la var neuro "Neuroticism"
	

**********
* SURVEY *
**********

forvalues i = 1(1)7{
	rename q`i' survey`i'
	replace survey`i' = survey`i'+1 //values from 0 to 6 in zTree
}	
	
la var survey1 "Reprimand" 
	la def survey1lbl 1 "not hurt at all" 2 "not hurt" 3 "somewhat not hurt" ///
		4 "Neutral" 5 "somewhat hurt" 6 "hurt" 7 "very hurt"
	la val survey1 survey1lbl

la var survey2 "Compliment"
	la def survey2lbl 1 "not happy at all" 2 "not happy" 3 "somewhat not happy" ///
		4 "Neutral" 5 "somewhat happy" 6 "happy"  7 "very happy"
	la val survey2 survey2lbl

la var survey3 "2nd Party Pun"
	la def survey3lbl 1 "not acceptable at all" 2 "not acceptable" ///
	3 "somewhat not acceptable" 4 "Neutral" 5 "somewhat socially acceptable" ///
	6 "socially acceptable" 7 "completely socially acceptable"
		la val survey3 survey3lbl
	
la var survey4 "3rd Party Pun"
	la def survey4lbl 1 "not acceptable at all" 2 "not acceptable" ///
	3 "somewhat not acceptable" 4 "Neutral" 5 "somewhat socially acceptable" ///
	6 "socially acceptable" 7 "completely socially acceptable"
		la val survey4 survey4lbl

la var survey5 "2nd Party C-Pun"
	la def survey5lbl 1 "not likely at all" 2 "not likely" 3 "somewhat not likely" ///
	4 "Neutral" 5 "somewhat likely" 6 "likely"  7 "very likely"
		la val survey5 survey5lbl

la var survey6 "3rd Party C-Pun"
	la def survey6lbl 1 "not likely at all" 2 "not likely" 3 "somewhat not likely" ///
	4 "Neutral" 5 "somewhat likely" 6 "likely"  7 "very likely"
		la val survey6 survey6lbl

la var survey7 "Standing"
	la def survey7lbl 1 "not likely at all" 2 "not likely" 3 "somewhat not likely" ///
	4 "Neutral" 5 "somewhat likely" 6 "likely"  7 "very likely"
		la val survey7 survey7lbl	
	
***************
* SAVING DATA *
***************
sort treat session group 
order session_code session treat group subject player
	
save counterpunish_survey.dta, replace


*****************************
* COUNTERPUNISHMENT DATASET *
*****************************
use counterpunish_choices.dta, clear
merge 1:1 session subject using counterpunish_survey.dta
save counterpunish.dta, replace

********************************************************************************
exit, clear
********************************************************************************
